(local header "<div id=\"preamble\" class=\"status\">
 <div id=\"banner\">
  <h1><a href=\"/home.html\">Kevin \"The Nuclear\" Bloom</a></h1>
  <hr />
  <div id=\"navit\">
    <a href=\"/contact.html\">Contact</a>
    &nbsp;
    <a href=\"/blog.html\">Blog</a>
    &nbsp;
    <a href=\"/projects.html\">Projects</a>
    &nbsp;
    <a href=\"/about-me.html\">About Me</a>
  </div>
 </div>
</div>
")

(local footer "<div id=\"footer\">
  <hr />
  <p>
   <a href=\"https://anybrowser.org/campaign/\">
    <img src=\"/img/any-browser.png\" alt=\"Viewable with any browser! No JS, no cookies, no bullshit!\"/></a>
    &nbsp;
    <a href=\"http://validator.w3.org/check?uri=nuclearkev.org\"><img
      src=\"http://www.w3.org/Icons/valid-xhtml10\" alt=\"Valid XHTML 1.0 Strict\" height=\"31\" width=\"88\" /></a>
    &nbsp;
    <a href=\"http://NetBSD.org/\"><img
      src=\"https://www.netbsd.org/images/powered-by-NetBSD.png\" alt=\"NetBSD\" width=\"88\" /></a>
  </p>
  <p>
    Copyright © 2017-2022 Kevin \"The Nuclear\" Bloom
  </p>
</div>
")

(fn top-stuff [title]
    (..
     "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">
<head>
"
     title
     "<link rel=\"shortcut icon\" type=\"image/png\" href=\"/img/guy-in-space.png\"/>
<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" />
<meta name=\"generator\" content=\"Org mode\" />
<meta name=\"author\" content=\"Kevin &quot;The Nuclear&quot; Bloom\" />
<meta http-equiv=\"cache-control\" content=\"no-cache, must-revalidate, post-check=0, pre-check=0\" />
<meta http-equiv=\"cache-control\" content=\"max-age=0\" />
<meta http-equiv=\"expires\" content=\"0\" />
<meta http-equiv=\"expires\" content=\"Tue, 01 Jan 1980 1:00:00 GMT\" />
<meta http-equiv=\"pragma\" content=\"no-cache\" />
<link rel='stylesheet' href='/styles/main.2.css' />
</head>
"))

;; -- From my str.fnl lib -----------------------

(fn drop [s ind]
    (string.sub s ind))

(fn take [s ind]
    (string.sub s 1 ind))

(fn substring-between [s fir lst]
  "Returns a substring of S defined by 2 strings, FIR and LST.
   The benefit of this is that if there are other instances of LST before the
   instance you want, they won't get caught.
   I.e.: You wish to get the value of the second line of \"this \n is a \n test\",
   You would just use (string-between str \"is a\" \"\n\")"
  (let [fir-pos (string.find s fir)
        up-to-fir (drop s fir-pos)
        lst-pos (string.find up-to-fir lst)]
    (take up-to-fir lst-pos)))

;; -- end -----------------------------------------

(fn generate-page [file-name]
  (let [file-port (io.open file-name "r")
        full-file (file-port:read "*a")
        title (.. (substring-between full-file "<title>" "</title>") "/title>\n")
        body (drop (substring-between full-file "<body>" "\n</body>") 7)]
    (file-port:close)
    (print (.. (top-stuff title)
               "<body>\n"
               header
               body
               footer
               "</body>\n"
               "</html>"))))

(generate-page (. arg 1))
